﻿namespace OleDbDAL.Processing
{
    using DBUtility;
    using IDal.Processing;
    using Model.Processing;
    using System;
    using System.Data;
    using System.Text;

    public class TeaProcessingApp : ITeaProcessing
    {
        private string dbname = "Systems";

        public void Add(TeaProcessingEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_TeaProcessing(");
            builder.Append("ChainNum,ProcessingNum,StockId,CommId,CommName,CommUnit,Weight,ProcessingMan,ProcessingTime,IsCheck,CheckMan,CheckTime,Remark");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.ProcessingNum + "',");
            builder.Append("" + model.StockId + ",");
            builder.Append("" + model.CommId + ",");
            builder.Append("'" + model.CommName + "',");
            builder.Append("" + model.CommUnit + ",");
            builder.Append("" + model.Weight + ",");
            builder.Append("" + model.ProcessingMan + ",");
            builder.Append("'" + model.ProcessingTime + "',");
            builder.Append("" + model.IsCheck + ",");
            builder.Append("" + model.CheckMan + ",");
            builder.Append("'" + model.CheckTime + "',");
            builder.Append("'" + model.Remark + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int ProcessingId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_TeaProcessing ");
            builder.Append(" where ProcessingId=" + ProcessingId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_TeaProcessing ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by ProcessingId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "ProcessingId", "T_TeaProcessing", swhere);
        }

        public TeaProcessingEnt GetModel(int ProcessingId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ProcessingId,ChainNum,ProcessingNum,StockId,CommId,CommName,CommUnit,Weight,ProcessingMan,ProcessingTime,IsCheck,CheckMan,CheckTime,Remark from T_TeaProcessing ");
            builder.Append(" where ProcessingId=" + ProcessingId);
            TeaProcessingEnt ent = new TeaProcessingEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.ProcessingId = ProcessingId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.ProcessingNum = table.Rows[0]["ProcessingNum"].ToString();
                if (table.Rows[0]["StockId"].ToString() != "")
                {
                    ent.StockId = int.Parse(table.Rows[0]["StockId"].ToString());
                }
                if (table.Rows[0]["CommId"].ToString() != "")
                {
                    ent.CommId = int.Parse(table.Rows[0]["CommId"].ToString());
                }
                ent.CommName = table.Rows[0]["CommName"].ToString();
                if (table.Rows[0]["CommUnit"].ToString() != "")
                {
                    ent.CommUnit = int.Parse(table.Rows[0]["CommUnit"].ToString());
                }
                if (table.Rows[0]["Weight"].ToString() != "")
                {
                    ent.Weight = decimal.Parse(table.Rows[0]["Weight"].ToString());
                }
                if (table.Rows[0]["ProcessingMan"].ToString() != "")
                {
                    ent.ProcessingMan = int.Parse(table.Rows[0]["ProcessingMan"].ToString());
                }
                if (table.Rows[0]["ProcessingTime"].ToString() != "")
                {
                    ent.ProcessingTime = DateTime.Parse(table.Rows[0]["ProcessingTime"].ToString());
                }
                if (table.Rows[0]["IsCheck"].ToString() != "")
                {
                    ent.IsCheck = int.Parse(table.Rows[0]["IsCheck"].ToString());
                }
                if (table.Rows[0]["CheckMan"].ToString() != "")
                {
                    ent.CheckMan = int.Parse(table.Rows[0]["CheckMan"].ToString());
                }
                if (table.Rows[0]["CheckTime"].ToString() != "")
                {
                    ent.CheckTime = DateTime.Parse(table.Rows[0]["CheckTime"].ToString());
                }
                ent.Remark = table.Rows[0]["Remark"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_TeaProcessing ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "ProcessingId");
        }

        public void IsCheck(TeaProcessingEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_TeaProcessing set ");
            builder.Append("IsCheck=" + model.IsCheck + ",");
            builder.Append("CheckMan=" + model.CheckMan + ",");
            builder.Append("CheckTime='" + model.CheckTime + "'");
            builder.Append(" where ProcessingId=" + model.ProcessingId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Update(TeaProcessingEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_TeaProcessing set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("ProcessingNum='" + model.ProcessingNum + "',");
            builder.Append("StockId=" + model.StockId + ",");
            builder.Append("CommId=" + model.CommId + ",");
            builder.Append("CommName='" + model.CommName + "',");
            builder.Append("CommUnit=" + model.CommUnit + ",");
            builder.Append("Weight=" + model.Weight + ",");
            builder.Append("ProcessingMan=" + model.ProcessingMan + ",");
            builder.Append("ProcessingTime='" + model.ProcessingTime + "',");
            builder.Append("IsCheck=" + model.IsCheck + ",");
            builder.Append("CheckMan=" + model.CheckMan + ",");
            builder.Append("CheckTime='" + model.CheckTime + "',");
            builder.Append("Remark='" + model.Remark + "'");
            builder.Append(" where ProcessingId=" + model.ProcessingId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

